<template>
  <view class="container info-container">
	<view class="duihuan-container">
			<my-tab :list="dhList" bgColor="#fff" :current="dhCurrentIndex" @itemClick='dhItemClick'></my-tab>
			<view class="line"></view>
			<view class="cate-container cate" v-if="dhIndex==0">
				<scroll-view scroll-y scroll-with-animation class="left-view">
					<view v-for="(item,index) in piao_list" :key="index" class="left-view-item"
						:class="{'left-view-item-active':piao_id==item.id}" @tap.stop="cateClick(item)">
						<text class="u-line-1">{{item.name}}</text>
					</view>
				</scroll-view>
				<scroll-view scroll-y scroll-with-animation class="right-view" @scrolltolower="loadGoods">
					<block v-if="goods_list.length>0">
					<view class="goods-row-item" v-for="(item,index) in goods_list" :key="index"
						@tap='toGoodsDetail(item)'>
						<image :src="item.goods_img" mode="widthFix" class="goods-img"></image>
						<view class="right">
							<text class="goods-name">{{item.goods_name}}</text>
							<view class="right-bot">
								<view class="bot_bot_one">
									<text>￥</text>
									<text class="price">{{item.goods_price}}</text>
								</view>
								<text class="bot_bot_three">销量：{{item.sales_sum}}</text>
							</view>
						</view>
					</view>
					</block>
					<view class="empty-container" v-else>
						<u-empty text="暂无相关商品" icon="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/common/empty.png"/>
					</view>
				</scroll-view>
			</view>
			<view class="order-container" v-if="dhIndex==1">
				<scroll-view scroll-y class="scroll" @scrolltolower="loadMoreOrder">
					<view class="order-items" v-if="order_list.length>0">
					<view class="order-item" v-for="(item,index) in order_list" :key="index" @click="toJhDetail(item,index)">
						<view class="top">
							<view class="left">{{item.time_text}}</view>
							<view class="right">{{item.status_text}}</view>
						</view>
						<view class="order-goods-rows">
							<view class="order-goods-row-item">
								<image :src="item.goods_img" mode="widthFix" class="goods-img"></image>
								<view class="right">
									<view class="goods-name">{{item.goods_name}}</view>
									<view class="right-bot">
										<view class="price">
											<text class="large">{{item.goods_price}}</text>
											<text>/{{item.pay_name}}</text>
										</view>
										<view class="num">x{{item.goods_num}}</view>
									</view>
								</view>
							</view>
						</view>
						<view class="total">
							<text class="black">实付:</text>
							<text class="large">{{item.order_amount}}</text>
							<text>/{{item.pay_name}}</text>
						</view>
						<view class="order-bot">
							<button size="mini">查看订单</button>
							<button size="mini" class="confirm" v-if="item.order_status==2" @click.stop="jh_order_confirm_show(item)">确认收货</button>
						</view>
					</view>
					<u-loadmore :status="order_hasMore?'loading':'nomore'" />
					</view>
					<view class="empty-container" v-else>
						<u-empty text="暂无相关订单" icon="https://farm-city.oss-cn-hangzhou.aliyuncs.com/static/common/empty.png"/>
					</view>
				</scroll-view>
			</view>
		</view>
		<u-popup :safeAreaInsetBottom="false" :safeAreaInsetTop="false" mode="center" :show="sh_show" round="5"
			:overlay="true" :customStyle="customStyle1" @close="sh_show=false">
			<view class="u-popup-slot">
				<text class="u-popup-slot_text">确认收货？</text>
				<view class="u-popup-slot_flex">
					<button @click="sh_show = !sh_show" class="cancel">取消</button>
					<button @click="jh_order_confirm()" class="confirm">确认</button>
				</view>
			</view>
		</u-popup>
	</view>
  </view>
</template>

<script>
var _this;
export default {
  data() {
    return {
		customStyle1: {
			'width': '80%'
		},
		dhList: [{
			name: '进货产品',
		}, {
			name: '进货订单',
		}],
		dhCurrentIndex: 0,
		dhIndex:0,
		yet_piao_load:false,
		piao_list:[],
		piao_id:0,
		goods_page:1,
		goods_hasMore:true,
		goods_list:[],
		yet_order_load:false,
		order_page:1,
		order_hasMore:true,
		order_list:[],
		temp_order:{},
		sh_show:false
    };
  },
  methods:{
	  jh_order_confirm_show(item){
	  	_this.temp_order=item
	  	_this.sh_show=true
	  },
	  async jh_order_confirm(){
	  	const params = {
	  		id:_this.temp_order.order_id,
	  	}
	  	//发送请求
	  	const data = await _this.$post('api/jh_dd_confirm',params)
	  	if(data.code==200){
	  		uni.$u.toast(data.msg)
	  		_this.temp_order['order_status']=3
	  		_this.temp_order['status_text']='已完成'
	  		_this.sh_show=false
	  	}else{
	  		uni.$u.toast(data.msg)
	  	}
	  },
	dhItemClick(e){
		_this.dhIndex=e
		if(e==1 && !_this.yet_order_load){
			_this.get_jh_dd_list()
		}
	},
	loadMoreOrder(){
		if(_this.order_hasMore){
			_this.order_page++
			_this.get_jh_dd_list()
		}
	},
	toJhDetail(item,index){
		uni.navigateTo({
			url:`/pages/order/jh-order-detail?id=${item.order_id}&index=${index}`
		})
	},
	async get_piao_list() {
		const params = {
			is_all:1
		}
		const data = await _this.$post('api/get_piao_list',params)
		if (data.code == 200){
			_this.yet_piao_load=true
			_this.piao_list=data.data.list
			if(_this.piao_list.length>0){
				_this.piao_id=_this.piao_list[0]['id']
			}
			_this.get_jh_goods()
		}else{}
	},
	async get_jh_goods(){
		const params = {
			pageIndex: _this.goods_page,
			pageSize:20,
			wid:_this.piao_id,
		}
		const data = await _this.$post('api/get_jh_goods',params)
		if(data.code==200){
			this.goods_list.push(...data.data.list)
			this.goods_hasMore=data.data.list.length>=params.pageSize
		}else{
			this.goods_hasMore=false
		}
	},
	cateClick(item){
		if(_this.piao_id==item.id){
			return
		}
		_this.piao_id=item.id
		_this.goods_page=1
		_this.goods_list=[]
		_this.get_jh_goods()
	},
	toGoodsDetail(item){
		uni.navigateTo({
			url:`/pages/goods/jh-goods-detail?id=${item.goods_id}`
		})
	},
	async get_jh_dd_list(){
		const params = {
			pageIndex: _this.order_page,
			pageSize:20,
		}
		const data = await _this.$post('api/get_jh_dd_list',params)
		if(data.code==200){
			_this.yet_order_load=true
			this.order_list.push(...data.data.list)
			this.order_hasMore=data.data.list.length>=params.pageSize
		}else{
			this.order_hasMore=false
		}
	}
  },
  onLoad(e) {
    _this=this;
	uni.$on('jhOrderConfirm',index=>{
		_this.order_list[index]['order_status']=3
		_this.order_list[index]['status_text']='已完成'
	})
	_this.get_piao_list()
  },
  onUnload() {
  	uni.$off('jhOrderConfirm')
  },
  onShow() {

  },
  components:{}
}
</script>

<style lang="scss" scoped>
	@import "@/static/css/deal.scss";
	@import "@/static/css/order.scss";
	@import "@/static/css/goods.scss";
	.container {
		position: relative;
		padding: 0;
		height: 100%;
	}
</style>
